﻿using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace D.Wx.SwaggerUtil
{
    public static class SwaggerUtil
    {
        /// <summary>
        /// 添加API文档中间件
        /// </summary>
        /// <param name="services"></param>
        /// <param name="basePath">为 Swagger JSON and UI设置xml文档注释路径</param>
        public static void AddSwagger(this IServiceCollection services, string basePath, string[] fileNames)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "接口文档",
                    Contact = new OpenApiContact()
                    {
                        Email = "d@qq.com",
                        Name = "d",
                        Url = new Uri("http://www.d.com")
                    },
                    License = new OpenApiLicense()
                    {
                        Name = "d",
                        Url = new Uri("http://www.d.com"),
                    }
                });

                //实体层
                //c.IncludeXmlComments(Path.Combine(basePath, entityName));

                //业务逻辑层
                //c.IncludeXmlComments(Path.Combine(basePath, "D.IService.xml"));

                //控制器层
                //var path = AppDomain.CurrentDomain.BaseDirectory;
                for (int i = 0; i < fileNames.Length; i++)
                {
                    c.IncludeXmlComments(Path.Combine(basePath, fileNames[i]), true);
                }

                //添加设置Token的按钮
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "Value: Bearer {token}",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });

                //添加Jwt验证设置
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                    {
                        {
                            new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference
                                {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                },
                                Scheme = "oauth2",
                                Name = "Bearer",
                                In = ParameterLocation.Header,
                            },
                            new List<string>()
                        }
                    });

            });
        }

        /// <summary>
        /// 使用API文档中间件
        /// 访问地址：http://127.0.0.1:5000/index.html
        /// </summary>
        /// <param name="app"></param>
        public static void UseMySwagger(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {

                // c.InjectStylesheet("/swagger-ui/custom.css");// 自定义css
                c.DocumentTitle = "Api接口";
                c.RoutePrefix = string.Empty;//直接根目录访问
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "1.0");
            });
        }


        /// <summary>
        /// 使用API文档中间件
        /// 访问地址：http://127.0.0.1:5000/index.html#/home
        /// https://doc.xiaominfo.com/docs/quick-start
        /// </summary>
        /// <param name="app"></param>
        public static void UseMyKnife4j(this IApplicationBuilder app)
        {
            app.UseKnife4UI(c =>
            {
                c.DocumentTitle = "Api接口";
                c.RoutePrefix = string.Empty; ; // 直接根目录访问
                c.SwaggerEndpoint("/v1/api-docs", "1.0");
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapSwagger("{documentName}/api-docs");
            });
        }
    }
}
